<%@ page language="java" import="java.util.*" import="java.sql.*" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<%@ page import="com.example.demo2.DBUtil" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>后台管理系统</title>
</head>
<link rel="stylesheet" href="css/bootstrap.css">
<body>
<style>
    .center{display: flex !important;align-content: center;justify-content: center;align-items: center;}
</style>
<div style="display: flex; justify-content: center;flex-direction: column;align-items: center;">
    <%@include file="admin_header.jsp"%>
    <div style="margin-top: 20px">
        <div style="display: flex;justify-content: space-between;">
            <button type="button" class="btn btn-primary" style="margin-bottom: 20px" data-toggle="modal" data-target="#exampleModal">添加电影</button>
            <a href="admin.jsp"><button type="button" class="btn btn-success" style="margin-bottom: 20px;">刷新</button></a>
        </div>
        <ul class="list-group list-group-horizontal">
            <li class="list-group-item" style="width: 50px">ID</li>
            <li class="list-group-item"style="width: 150px">电影名称</li>
            <li class="list-group-item"style="width: 150px">海报</li>
            <li class="list-group-item"style="width: 150px">类型</li>
            <li class="list-group-item"style="width: 100px">评分</li>
            <li class="list-group-item"style="width: 150px">上映时间</li>
            <li class="list-group-item"style="width: 150px">票房</li>
            <li class="list-group-item"style="width: 150px">地区/时长</li>
            <li class="list-group-item"style="width: 150px">副名</li>
            <li class="list-group-item"style="width: 150px">主演</li>
            <li class="list-group-item"style="width: 150px">操作</li>
        </ul>

        <%
            request.setCharacterEncoding("UTF-8");
            String wenj = request.getParameter("wenj");
            String a_name = request.getParameter("name");
            String a_number = request.getParameter("number");
            String a_leix = request.getParameter("leix");
            String a_time = request.getParameter("time");
            String a_piaof  = request.getParameter("piaof");
            String a_diqandshic = request.getParameter("diqandshic");
            String a_name2  = request.getParameter("name2");
            String a_zhuyan = request.getParameter("zhuyan");
            String u_name = request.getParameter("u_name");
            String u_number = request.getParameter("u_number");
            String u_id = request.getParameter("u_id");
            String u_leix = request.getParameter("u_leix");
            String u_time = request.getParameter("u_time");
            String u_piaof  = request.getParameter("u_piaof");
            String u_diqandshic = request.getParameter("u_diqandshic");
            String u_name2  = request.getParameter("u_name2");
            String u_zhuyan = request.getParameter("u_zhuyan");
            String u_wenj = request.getParameter("u_wenj");
            String d_id = request.getParameter("d_id");

            Connection cn = null;
            PreparedStatement ps = null;

            ResultSet rs = null;
            String sql = "";
            cn = DBUtil.getConnection();
            sql = "select * from goods";
            ps = cn.prepareStatement(sql);
            rs = ps.executeQuery();
            System.out.println(wenj + a_name);
            if (wenj != null && a_name !=null){
                System.out.println(123);
                int n=0;
                sql = "insert into goods(name,img,number,leix,time,piaof,diqandshic,name2,zhuyan) values(?,?,?,?,?,?,?,?,?)";
                ps = cn.prepareStatement(sql);
                ps.setObject(1, a_name);
                ps.setObject(2, wenj);
                ps.setObject(3, a_number);
                ps.setObject(4, a_leix);
                ps.setObject(5, a_time);
                ps.setObject(6, a_piaof);
                ps.setObject(7, a_diqandshic);
                ps.setObject(8, a_name2);
                ps.setObject(9, a_zhuyan);
                n= ps.executeUpdate();
                response.sendRedirect("admin.jsp");
            }
            if (u_name != null){
                System.out.println(456);
                int n=0;
                sql = "UPDATE goods SET name =\""+u_name+"\", img =\""+ u_wenj +"\" "+ ",number=" + u_number + ",leix=\"" + u_leix +
                        "\",time=\""+ u_time + "\",piaof=\"" + u_piaof + "\",diqandshic=\"" + u_diqandshic + "\",name2=\"" + u_name2 +
                        "\",zhuyan=\"" + u_zhuyan + "\" WHERE id=" + u_id;
                ps = cn.prepareStatement(sql);
                System.out.println(sql);
                n= ps.executeUpdate();
                response.sendRedirect("admin.jsp");
            }
            if (d_id != null){
                int n=0;
                sql = "DELETE FROM `gp`.`goods` WHERE `id` = " + d_id;
//              System.out.println(sql);
                ps = cn.prepareStatement(sql);
                n= ps.executeUpdate();
                response.sendRedirect("admin.jsp");
            }
            while(rs.next()){
                int id = rs.getInt("id");
                String name = rs.getString("name");
                String img = rs.getString("img");
                String number = rs.getString("number");
                String leix = rs.getString("leix");
                String time = rs.getString("time");
                String piaof = rs.getString("piaof");
                String diqandshic = rs.getString("diqandshic");
                String name2 = rs.getString("name2");
                String zhuyan = rs.getString("zhuyan");

        %>
        <ul class="list-group list-group-horizontal-sm">
            <li class="list-group-item center"style="width: 50px;"><%=id%></li>
            <li class="list-group-item center"style="width: 150px"><%=name%></li>
            <li class="list-group-item center"style="width: 150px">
                <img src="<%=img%>" width="140px" height="194px">
            </li>
            <li class="list-group-item center"style="width: 150px"><%=leix%></li>
            <li class="list-group-item center"style="width: 100px"><%=number%></li>
            <li class="list-group-item center"style="width: 150px"><%=time%></li>
            <li class="list-group-item center"style="width: 150px"><%=piaof%></li>
            <li class="list-group-item center"style="width: 150px"><%=diqandshic%></li>
            <li class="list-group-item center"style="width: 150px"><%=name2%></li>
            <li class="list-group-item center"style="width: 150px"><%=zhuyan%></li>
            <li class="list-group-item center"style="width: 150px">
                <button type="button" class="btn btn-primary btn-sm"
                        data-toggle="modal" data-target="#upadminModal<%=id%>"
                        style="margin-bottom: 20px;margin-right: 5px">修改</button>
                <a href="admin.jsp?d_id=<%=id%>" type="button" class="btn btn-info btn-sm" style="margin-bottom: 20px">删除</a>

            </li>
        </ul>

        <%@include file="up_admin.jsp"%>
        <%
            }
        %>
    </div>


    <!-- Modal -->
    <div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered modal-dialog-scrollable">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel">添加电影</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <%if (wenj != null){%>
                    <label for="img">海报:</label>
                    <img src="<%=wenj%>" width="140px">
                    <%}else{%>
                    <form method="POST" action="updata.jsp" enctype="multipart/form-data">
                        <label for="img">海报:</label>
                        <input TYPE="FILE" NAME="FILE1" required SIZE="30" id="img">
                        <input TYPE="SUBMIT" VALUE="开始上传">
                    </form>
                    <%}%>
                    <form action="admin.jsp" method="post">
                        <div class="form-group">
                            <label for="name">电影名:</label>
                            <input type="text" name="name" required class="form-control" id="name">
                        </div>
                        <div class="form-group" style="display: none">
                            <label for="wenj">海报:</label>
                            <input type="text" name="wenj" value="<%=wenj%>" class="form-control" id="wenj">
                        </div>
                        <div class="form-group">
                            <label for="number">评分:</label>
                            <input type="text" name="number" required class="form-control"
                                   id="number">
                        </div>
                        <div class="form-group">
                            <label for="leix">类型:</label>
                            <input type="text" name="leix" required class="form-control" id="leix">
                        </div>
                        <div class="form-group">
                            <label for="time">上映时间:</label>
                            <input type="datetime-local" name="time" required class="form-control"
                                   id="time">
                        </div>
                        <div class="form-group">
                            <label for="piaof">票房:</label>
                            <input type="text" name="piaof" required class="form-control"
                                   id="piaof">
                        </div>
                        <div class="form-group">
                            <label for="diqandshic">地区/时长:</label>
                            <input type="text" name="diqandshic" required class="form-control" id="diqandshic">
                        </div>
                        <div class="form-group">
                            <label for="name2">副名:</label>
                            <input type="text" name="name2" required class="form-control"
                                   id="name2">
                        </div>
                        <div class="form-group">
                            <label for="zhuyan">主演:</label>
                            <input type="text" name="zhuyan" required class="form-control"
                                   id="zhuyan">
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                            <button type="submit" class="btn btn-primary">确认</button>
                        </div>
                    </form>
                </div>

            </div>
        </div>
    </div>
<script>
    <%
     String x = request.getParameter("x");
        if (wenj != null){
            if (x !=null){
    %>
    $('#upadminModal<%=request.getParameter("x")%>').modal("show");
    <%
        }else {
          %>
                $('#exampleModal').modal("show");
    <%
        }}%>
</script>
</div>
<div style="height: 50px"></div>
<script src="js/jQuery.js"></script>
<script src="js/bootstrap.js"></script>

</body>
</html>